package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.torefactor.PopupStructureElementUtil;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * 
 * <pre>
 * 	<xsd:complexType name="popupStructure">
 * 		<xsd:choice minOccurs="1" maxOccurs="unbounded">
 * 			<xsd:group ref="hierElements" />
 * 			<xsd:group ref="blockElements" />
 * 			<xsd:element ref="intro" />
 * 			<xsd:element ref="wrap" />
 * 			<xsd:element ref="heading" />
 * 			<xsd:element ref="subheading" />
 * 			<xsd:element ref="content" />
 * 			<xsd:element ref="num" />
 * 			<xsd:group ref="containerElements" />
 * 			<xsd:group ref="ANcontainers" />
 * 			<xsd:element ref="recital" />
 * 			<xsd:element ref="citation" />
 * 		</xsd:choice>
 * 		<xsd:attributeGroup ref="corereq" />
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 */
public abstract class PopupStructure extends CoreReqImpl {

	// Mandatory (min 1).
	private final List<PopupStructureElement> _elements = new ArrayList<PopupStructureElement>(4);

	public List<PopupStructureElement> getElements() {
		return _elements;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		PopupStructureElementUtil.read(in, _elements);
	}

	protected final void internalAccept(AKVisitor visitor) {
		for (int i = 0, n = _elements.size(); i < n; i++) {
			_elements.get(i).accept(visitor);
		}
	}

}